import { defineStore } from 'pinia'
import { useAuthStore } from './auth';
import { useCloudStorageStore } from './cloudStorage';
import { useAIModelStore } from './aiModel';

export interface DataItem {
  id: number
  name: string
  category: string
  status: 'active' | 'inactive'
  createTime: string
  updateTime: string
  description: string
}

export interface DataState {
  dataList: DataItem[]
  loading: boolean
  currentItem: DataItem | null
  totalCount: number
}

export const useDataStore = defineStore('data', {
  state: (): DataState => ({
    dataList: [],
    loading: false,
    currentItem: null,
    totalCount: 0
  }),
  
  getters: {
    getDataList: (state) => state.dataList,
    getDataById: (state) => (id: number) => {
      return state.dataList.find(item => item.id === id) || null
    },
    getActiveData: (state) => {
      return state.dataList.filter(item => item.status === 'active')
    }
  },
  
  actions: {
    // 加载数据列表
    async fetchDataList() {
      this.loading = true
      try {
        // 这里模拟API调用
        await new Promise(resolve => setTimeout(resolve, 800))
        
        // 模拟数据
        const mockData: DataItem[] = Array.from({ length: 20 }).map((_, index) => ({
          id: index + 1,
          name: `数据项 ${index + 1}`,
          category: ['类型A', '类型B', '类型C'][Math.floor(Math.random() * 3)],
          status: Math.random() > 0.3 ? 'active' : 'inactive',
          createTime: new Date(Date.now() - Math.random() * 10000000000).toISOString(),
          updateTime: new Date(Date.now() - Math.random() * 1000000000).toISOString(),
          description: `这是数据项 ${index + 1} 的描述信息`
        }))
        
        this.dataList = mockData
        this.totalCount = mockData.length
      } catch (error) {
        console.error('获取数据列表失败:', error)
      } finally {
        this.loading = false
      }
    },
    
    // 设置当前数据项
    setCurrentItem(item: DataItem | null) {
      this.currentItem = item
    },
    
    // 添加数据项
    addDataItem(item: Omit<DataItem, 'id' | 'createTime' | 'updateTime'>) {
      const newItem: DataItem = {
        ...item,
        id: this.dataList.length > 0 ? Math.max(...this.dataList.map(d => d.id)) + 1 : 1,
        createTime: new Date().toISOString(),
        updateTime: new Date().toISOString()
      }
      
      this.dataList.push(newItem)
      this.totalCount++
      return newItem
    },
    
    // 更新数据项
    updateDataItem(id: number, updates: Partial<Omit<DataItem, 'id' | 'createTime'>>) {
      const index = this.dataList.findIndex(item => item.id === id)
      if (index !== -1) {
        this.dataList[index] = {
          ...this.dataList[index],
          ...updates,
          updateTime: new Date().toISOString()
        }
        
        // 如果更新的是当前选中的项，也更新currentItem
        if (this.currentItem && this.currentItem.id === id) {
          this.currentItem = this.dataList[index]
        }
      }
    },
    
    // 删除数据项
    deleteDataItem(id: number) {
      const index = this.dataList.findIndex(item => item.id === id)
      if (index !== -1) {
        this.dataList.splice(index, 1)
        this.totalCount--
        
        // 如果删除的是当前选中的项，清空currentItem
        if (this.currentItem && this.currentItem.id === id) {
          this.currentItem = null
        }
      }
    }
  }
})

export {
  useAuthStore,
  useCloudStorageStore,
  useAIModelStore
}; 